package com.laikan.bos.web.action.base;

import com.laikan.bos.domain.base.Standard;
import com.laikan.bos.service.base.StandardService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/11/10.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{

    private Standard standard = new Standard();

    @Autowired
    private StandardService standardService;

    @Override
    public Standard getModel(){

        return standard;
    }

    @Action(value = "standard_save",results = {@Result(name = "success",type = "redirect",location = "./pages/base/standard.html")})
    public String save(){
        System.out.println("save-------------------------");
        System.out.println(standard);
        standardService.save(standard);
        return SUCCESS;
    }

    private int page;
    private int rows;

    public void setPage(int page){

        this.page = page;
    }

    public void setRows(int rows){

        this.rows = rows;
    }

    @Action(value = "standard_pageQuery",results = {@Result(name = "success",type = "json")})
    public String pageQuery(){
        System.out.println("pageQuery-------------------------");
        Pageable pageable = new PageRequest(page-1,rows);

        Page<Standard> pageData = standardService.findPageData(pageable);

        Map<String,Object> result = new HashMap<>();
        result.put("total",pageData.getNumberOfElements());
        result.put("rows",pageData.getContent());
       /* System.out.println(standard);
        standardService.save(standard);*/
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }
}
